import { CallHandler, ExecutionContext, Injectable, NestInterceptor } from '@nestjs/common'
import { Observable } from 'rxjs'
import { map } from 'rxjs/operators'
import { Response } from 'express'
@Injectable()
export class ResponseInterceptor implements NestInterceptor {
	intercept(ctx: ExecutionContext, next: CallHandler): Observable<any> {
		const response = ctx.switchToHttp().getResponse<Response>()

		// console.log('app：响应拦截器', ctx.getHandler())

		return next.handle()

		return next.handle().pipe(
			map((response) => {
				// 在具体业务中也可以自行定制code

				console.log('app：响应拦截器', response)
				if (response.code) {
					const { code, data, msg } = response
					return {
						data,
						code,
						msg,
						success: true,
					}
				} else {
					const { data, message } = response
					return {
						data: message ? data : response,
						code: 0,
						message: message || 'success',
						success: true,
					}
				}
			})
		)
	}
}
